//验证码倒计时
var countdown=120;
function settime(obj) {
    if (countdown == 0) {
// obj.removeAttribute("disabled");
        obj.value="获取验证码";
        countdown = 120;
        return;
    } else {
        // obj.setAttribute("disabled", true);
        obj.value="重新发送(" + countdown + ")";
        countdown--;
    }
    setTimeout(function() {
            settime(obj) }
        ,1000)
}
//默认选中第一个
$('.currency').find("a:first").addClass('currency-active');
var coinId = Number($('.currency-active').data('p'));
$.ajax({
    url:'/wallet-balance.json',
    data:{coinId:coinId},
    dataType:'json',
    type:'get',
    success:function(data){
        $('.can-balance').html(data.balance);
    },
    error:function(err){
        $('.err-info').html(JSON.parse(err.responseText).errors[0].msg);
    }
})
//认购金额=认购份数*认购基准金额
$('.total-amount').html($('.bench').html());
$('.addition').click(function(){
    var bench = $('.bench').html();
    var copies = Number($('.copies-number').val())+1;
    $('.copies-number').val(copies);
    $('.total-amount').html(copies*bench);
})
$('.subtraction').click(function(){
    var bench = $('.bench').html();
    var copies = Number($('.copies-number').val())-1;
    if(copies>-1){
        $('.copies-number').val(copies);
        $('.total-amount').html(copies*bench);
    }else{
        $('.copies-number').val('0');
        $('.total-amount').html('0');
    }
});
//立即认筹
//未登录
$('.investment-btn-no').click(function(){
    $('.err-info').html('登录即可投资');
});
$('.investment-btn').click(function(){
    var projectId = Number($('.projectId').html());
    var coinId = Number($('.currency-active').data('p'));
    var proportion = Number($('.currency-active').data('proportion'));
    var tokenNumber = Number($('.total-amount').val());

    var coinNumberFix =(tokenNumber*proportion).toFixed(8);
    if(String(coinNumberFix).split('.')[1]){
        var coinNumber = Number(coinNumberFix);
    }else{
        //取小数点前的数
        var befor = String(coinNumberFix).split('.')[0];
        //取小数点后两位
        var after = String(coinNumberFix).split('.')[1].substring(0,2);
        var coinNumber = Number(befor+'.'+after);
    }


    var to = String($('.total-amount').val());
    var canBalance = Number($('.can-balance').html());
    var googleStatus = Number($('.googleStatus').data('google'));
    if(googleStatus==0){
        $('.err-info').html('请先去安全中心进行谷歌两步验证的设置');
    }else if(!tokenNumber){
        $('.err-info').html('投资金额不能为空');
    }
    // else if(googleStatus==0){
    //     $('.err-info').html('若投资，请先进行谷歌两步验证');
    // }
    else if(!coinId){
        $('.err-info').html('请选择币种');
    }else if(tokenNumber>canBalance){
        $('.err-info').html('投资金额大于可用余额');
    } else if(String(to).split('.')[1]){
        if(to.split('.')[1].length>2){
            $('.err-info').html('提币数量请不要超过小数点后2位');
        }else{
            $('.err-info').html('');
            var data = {
                projectId: projectId,
                coinId: coinId,
                coinNumber: tokenNumber,
                tokenNumber: coinNumber
            };
            $.ajax({
                url:"/order.json",
                type:"post",
                dataType:"json",
                contentType:"application/json",
                data:JSON.stringify(data),
                success:function(data){
                    console.log(data);
                    $('.cover').css('display','block');
                    $('.pay-btn').data('id',data.data.model.id);
                    $('.order-number').html(data.data.model.orderNo);
                    $('.order-bz').html(data.data.model.tokenName);
                    $('.order-sl').html(data.data.model.tokenNumber);
                    if(!String(data.data.model.coinNumber).split('.')[1]){
                        var coinNumbers = Number(data.data.model.coinNumber);
                    }else{
                        //取小数点前的数
                        var befor = String(data.data.model.coinNumber).split('.')[0];
                        //取小数点后两位
                        var after = String(data.data.model.coinNumber).split('.')[1].substring(0,2);
                        var coinNumbers = Number(befor+'.'+after);
                    }
                    $('.should-to').html(coinNumbers);
                    $('.should-name').html(data.data.model.coinName);
                    $('.order-name').html(data.data.model.name);
                },
                error: function(err){
                    $('.err-info').html(JSON.parse(err.responseText).errors[0].msg);
                }
            })
        }
    } else{
        var data = {
            projectId: projectId,
            coinId: coinId,
            coinNumber: tokenNumber,
            tokenNumber: coinNumber
        };
        $.ajax({
            url:"/order.json",
            type:"post",
            dataType:"json",
            contentType:"application/json",
            data:JSON.stringify(data),
            success:function(data){
                console.log(data);
                $('.cover').css('display','block');
                $('.pay-btn').data('id',data.data.model.id);
                $('.order-number').html(data.data.model.orderNo);
                $('.order-bz').html(data.data.model.tokenName);
                $('.order-sl').html(data.data.model.tokenNumber);
                $('.should-to').html(data.data.model.coinNumber);
                $('.should-name').html(data.data.model.coinName);
                $('.order-name').html(data.data.model.name);
            },
            error: function(err){
                $('.err-info').html(JSON.parse(err.responseText).errors[0].msg);
            }
        })
    }

    // if(!tokenNumber){
    //     $('.err-info').html('投资金额不能为空');
    // }else if(!coinId){
    //     $('.err-info').html('请选择币种');
    // }else if(tokenNumber>canBalance){
    //     $('.err-info').html('投资金额大于可用余额');
    // } else if(String(to).split('.')[1]){
    //     if(to.split('.')[1].length>2){
    //         $('.err-info').html('提币数量请不要超过小数点后2位');
    //     }else{
    //         $('.err-info').html();
    //         var data = {
    //             projectId: projectId,
    //             coinId: coinId,
    //             coinNumber: coinNumber,
    //             tokenNumber: tokenNumber
    //         };
    //         $.ajax({
    //             url:"/order.json",
    //             type:"post",
    //             dataType:"json",
    //             contentType:"application/json",
    //             data:JSON.stringify(data),
    //             success:function(data){
    //                 console.log(data);
    //                 $('.cover').css('display','block');
    //                 $('.pay-btn').data('id',data.data.model.id);
    //                 $('.order-number').html(data.data.model.orderNo);
    //                 $('.order-bz').html(data.data.model.tokenName);
    //                 $('.order-sl').html(data.data.model.tokenNumber);
    //                 if(!String(data.data.model.coinNumber).split('.')[1]){
    //                     var coinNumbers = Number(coinNumberFix);
    //                 }else{
    //                     //取小数点前的数
    //                     var befor = String(coinNumberFix).split('.')[0];
    //                     //取小数点后两位
    //                     var after = String(coinNumberFix).split('.')[1].substring(0,2);
    //                     var coinNumbers = Number(befor+'.'+after);
    //                 }
    //                 $('.should-to').html(coinNumbers);
    //                 $('.should-name').html(data.data.model.coinName);
    //                 $('.order-name').html(data.data.model.name);
    //             },
    //             error: function(err){
    //                 $('.err-info').html(JSON.parse(err.responseText).errors[0].msg);
    //             }
    //         })
    //     }
    // } else{
    //     var data = {
    //         projectId: projectId,
    //         coinId: coinId,
    //         coinNumber: coinNumber,
    //         tokenNumber: tokenNumber
    //     };
    //     $.ajax({
    //         url:"/order.json",
    //         type:"post",
    //         dataType:"json",
    //         contentType:"application/json",
    //         data:JSON.stringify(data),
    //         success:function(data){
    //             console.log(data);
    //             $('.cover').css('display','block');
    //             $('.pay-btn').data('id',data.data.model.id);
    //             $('.order-number').html(data.data.model.orderNo);
    //             $('.order-bz').html(data.data.model.tokenName);
    //             $('.order-sl').html(data.data.model.tokenNumber);
    //             $('.should-to').html(data.data.model.coinNumber);
    //             $('.should-name').html(data.data.model.coinName);
    //             $('.order-name').html(data.data.model.name);
    //         },
    //         error: function(err){
    //             $('.err-info').html(JSON.parse(err.responseText).errors[0].msg);
    //         }
    //     })
    // }
});

//
$('.coin-name').click(function(){
    $('.coin-name').removeClass('currency-active');
    $(this).addClass('currency-active');
    var coinId = $(this).data('p');
    $.ajax({
        url:'/wallet-balance.json',
        data:{coinId:coinId},
        dataType:'json',
        type:'get',
        success:function(data){
            console.log(data);
            $('.can-balance').html(data.balance);
        },
        error:function(err){
            $('.err-info').html('登录即可投资');
        }
    })
});

$('.order-option').click(function(){
    $('.cover').css('display','none');
    $('.google-password').val('');
});
//发送手机验证码
$('.send-code').click(function(){
    var th = this;
    $.ajax({
        url:'/order-pay-code.json',
        type: 'get',
        dataType: 'json',
        success: function (data1) {
            settime(th);
            $('.err-info').html('验证码已发送');
        },
        error:function(msg){
            $('.err-info').html(JSON.parse(msg.responseText).errors[0].msg);
        }
    })
});
//监听select
$('.google').change(function(){
    var verifyType = Number($("#google").find("option:selected").val());
    if(verifyType==2){
        $('.send-code').css('display','block');
    }else{
        $('.send-code').css('display','none');
    }
});
//立即支付
$('.pay-btn').click(function(){
    var id = $(this).data('id');
    var verifyCode= $('.google-password').val();
    var verifyType = Number($("#google").find("option:selected").val());
    if(verifyType==1){ //谷歌验证
        if(verifyCode==''){
            $('.err-info').html('谷歌身份验证码不能为空');
        }else{
            $.ajax({
                url: '/user/bind_google_secret.json',
                type: 'patch',
                dataType: 'json',
                data: {
                    secretCode: verifyCode
                },
                success: function (data1) {
                    $.ajax({
                        url:'/order.json',
                        data:{
                            id:id,
                            verifyType:verifyType,
                            verifyCode:verifyCode
                        },
                        dataType:'json',
                        type:'PATCH',
                        success:function(data){
                            location.href='/order?viewType=page';
                        },
                        error:function(err){
                            $('.err-info').html(JSON.parse(err.responseText).errors[0].msg);

                        }
                    })
                },
                error:function(msg){
                    $('.err-info').html(JSON.parse(msg.responseText).errors[0].msg);
                }
            });
        }
    }else{ //手机验证
        if(verifyCode==''){
            $('.err-info').html('手机验证码不能为空');
        }else{
            $.ajax({
                url:'/order.json',
                data:{
                    id:id,
                    verifyType:verifyType,
                    verifyCode:verifyCode
                },
                dataType:'json',
                type:'PATCH',
                success:function(data){
                    location.href='/order?viewType=page';
                },
                error:function(err){
                    $('.err-info').html(JSON.parse(err.responseText).errors[0].msg);

                }
            })
        }
    }
});


//全部投资 
$('.all-tz').click(function(){
    var balance = $('.can-balance').html();
    if(!String(balance).split('.')[1]){
        var balance1 = Number(balance);
    }else{
        //取小数点前的数
        var befor = String(balance).split('.')[0];
        //取小数点后两位
        var after = String(balance).split('.')[1].substring(0,2);
        var balance1 = Number(befor+'.'+after);
    }
    if(balance1==''){
        return 0;
    }else{
        $('.total-amount').val(balance1);
    }
});


